package com.study.system.service;

import com.study.system.entity.Activity;
import com.study.system.mapper.ActivityMapper;
import com.study.system.vo.ActivityInfoVO;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ActivityService {

    @Resource
    private ActivityMapper activityMapper;

    /**
     * 根据用户id查询所有参加的活动
     *
     * @param userId 用户id
     * @return 查询结果
     */
    public List<ActivityInfoVO> findAllActivityByUserID(Integer userId) {
        List<Integer> activityIds = activityMapper.selectActivityIdByUser(userId);
        List<ActivityInfoVO> activities = new ArrayList<>();
        for (Integer activityId : activityIds) {
            activities.add(activityMapper.selectActivityById(activityId));
        }
        return activities;
    }

    /**
     * 查询所有可以参加的活动
     *
     * @param userId 登录的用户id
     * @return 查询结果
     */
    public List<ActivityInfoVO> findAllActivityCanAttend(Integer userId) {
        List<ActivityInfoVO> activities;
        List<Integer> participated = activityMapper.selectActivityIdByUser(userId);
        if (!participated.isEmpty()) {
            activities = activityMapper.selectAllActivityNotId(participated);
        } else {
            activities = activityMapper.selectAllActivity();
        }
        return activities;
    }

    /**
     * 根据社团ID查询所有活动
     *
     * @param clubId 社团ID
     * @return 查询结果
     */
    public List<ActivityInfoVO> findAllClubActivity(Integer clubId) {
        return activityMapper.selectAllClubActivity(clubId);
    }

    /**
     * 插入活动信息
     *
     * @param userId   创建活动者ID
     * @param activity 活动信息
     * @return 操作结果
     */
    public Boolean insertActivity(Integer userId, Activity activity) {
        activity.setCreatorId(userId);
        return activityMapper.insertActivity(activity) != 0;
    }

    /**
     * 修改活动状态
     *
     * @param activityId 活动ID
     * @param newStatus  新的状态编号
     * @return 操作是否成功
     */
    public Boolean updateActivityStatus(Integer activityId, Integer newStatus) {
        return activityMapper.updateActivityStatus(activityId, newStatus) != 0;
    }

    /**
     * 用户参加活动
     *
     * @param userId 用户ID
     * @param activityId 社团ID
     * @return 操作是否成功
     */
    public Boolean userAttendActivity(Integer userId, Integer activityId) {
        return activityMapper.userAttendActivity(userId, activityId) == 1;
    }

    /**
     * 用户退出活动
     *
     * @param userId 用户Id
     * @param activityId 活动ID
     */
    public void userExitActivity(Integer userId, Integer activityId) {
        activityMapper.deleteUserActivity(userId, activityId);
    }

    public Boolean modifyActivityInfo(ActivityInfoVO activityInfo) {
        return activityMapper.modifyActivityInfo(activityInfo) == 1;
    }

    public List<ActivityInfoVO> searchActivityByWord(Integer userId, Map<String, String> word) {
        List<ActivityInfoVO> activities = new ArrayList<>();
        for (String key: word.keySet()) {
            List<ActivityInfoVO> tempActivities = activityMapper.selectActivityByWord(key, word.get(key),userId);
            activities.addAll(tempActivities);
        }
        return activities;
    }

    public List<ActivityInfoVO> searchCouldAttendActivityByWord(Integer userId, Map<String, String> word) {
        List<ActivityInfoVO> activities = new ArrayList<>();
        for (String key: word.keySet()) {
            List<ActivityInfoVO> tempActivities = activityMapper.selectCouldAttendActivityByWord(key, word.get(key),userId);
            activities.addAll(tempActivities);
        }
        return activities;
    }

    public List<ActivityInfoVO> searchCurActivityByWord(Map<String, String> word) {
        List<ActivityInfoVO> activities = new ArrayList<>();
        Integer clubId = Integer.valueOf(word.get("clubId"));
        for (String key: word.keySet()) {
            if (key.equals("clubId")) {
                continue;
            }
            List<ActivityInfoVO> tempActivities = activityMapper.selectCurClubActivityByWord(key, word.get(key), clubId);
            activities.addAll(tempActivities);
        }
        return activities;
    }
}
